import dayjs from "dayjs";
import Identicon from "identicon.js";
// @ts-ignore
import md5 from "blueimp-md5";

// 树型结构查找数据所在位置
function FindParent(arr, row) {
    let index = arr.findIndex(value => {
        return value.empty.id === row.empty.id
    });
    if (index === -1) {
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].children.length > 0) {
                let  p=FindParent(arr[i].children, row)
                if (p[1]!==-1){
                    return p
                }
            }
        }
    }
    return [arr, index]
}

const dateFormat = (val, format = "YYYY-MM-DD hh:mm:ss") => {
    if (!isNaN(val)) {
        val = parseInt(val);
    }
    return dayjs(val).format(format);
}

const Status = (ststus: number): string => {
    switch (ststus) {
        case 0:
            return '正常'
        case 1:
            return '停用'
    }
    return 'nil'
}
//随机头像
const RandomAvatar=():string=>{
    // create a base64 encoded PNG
    let hash=md5(String(Math.random() * 2))
    let options = {
        size: 500,                                // 420px square
        format: 'svg'                             // use SVG instead of PNG
    }
    // @ts-ignore
    let data=new Identicon(hash,options).toString();
    return "data:image/svg+xml;base64,"+data
}
export {
    FindParent,
    dateFormat,
    Status,
    RandomAvatar
}